package com.ioudi.digu.vo.adapter;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.ioudi.digu.vo.ProducedMessage;
import com.ioudi.digu.vo.message.MessageBody;
import com.ioudi.digu.vo.message.MessageHeader;

import flex.messaging.messages.AsyncMessage;
import flex.messaging.messages.Message;

public class ProducedMessageAdapter
{
    public ProducedMessage getProducedMessage(Message fm)
    {
    	ProducedMessage pm = new ProducedMessage();
    	if(fm != null)
    	{
    		pm.setMessageId(fm.getMessageId());	
    		MessageHeader headers = new MessageHeader();
    		
    		Map hdm = new HashMap<String, Object>();
    		
    		Map od = fm.getHeaders();
    		
    		Iterator<String> iterator = od.keySet().iterator();
    		String key;
    		while(iterator.hasNext())
    		{
    			key = iterator.next();
    			if(key.startsWith("DS"))
    			{
    				continue;
    			}
    			if(key.equals("contentType"))
        		{
    				pm.setContentType(String.valueOf(fm.getHeader(key)));
        			continue;
        		}
        		if(key.equals("channelType"))
        		{
        			pm.setChannelType(String.valueOf(fm.getHeader(key)));
        			continue;
        		}
        		if(key.equals("replyID"))
        		{
        			pm.setReplyID(String.valueOf(fm.getHeader(key)));
        			continue;
        		}
        		if(key.equals(MessageHeader.HEADER_FIELD_REPLY_USER))
        		{
        			pm.setReplyUser(String.valueOf(fm.getHeader(key)));
        			continue;
        		}
        		if(key.equals("receiver"))
        		{
        			pm.setReceiver(String.valueOf(fm.getHeader(key)));
        			continue;
        		}
        		hdm.put(key, od.get(key));
    		}
    		headers.setHeaders(hdm);
    		
    		pm.setHeaders(headers);
    		
    		MessageBody body = new MessageBody();
    		body.setBody(fm.getBody());
    		pm.setBody(body);
    	}
    	pm.setTimestamp(new Date());
    	return pm;
    }
    
    public Message getMessage(ProducedMessage message)
    {
    	if(message == null)
    	{
    		return null;
    	}
    	AsyncMessage am = new AsyncMessage();
    	am.setMessageId(message.getMessageId());
    	am.setTimestamp(message.getTimestamp().getTime());
    	am.setBody(message.getBody().getBody());
    	Map hds = message.getHeaders().getHeaders();
    	hds.put("user", new OutboundUser(message.getUser(), am));
    	hds.put(MessageHeader.HEADER_FIELD_CHANNEL_TYPE, message.getChannelType());
    	hds.put(MessageHeader.HEADER_FIELD_CONTENT_TYPE, message.getContentType());
    	hds.put(MessageHeader.HEADER_FIELD_LOCATION, message.getLocation());
    	if(message.getReceiver() != null)
    	{
    		hds.put(MessageHeader.HEADER_FIELD_RECEIVER, message.getReceiver());
    	}
    	if(message.getReceiver() != null)
    	{
    		hds.put(MessageHeader.HEADER_FIELD_RECEIVER, message.getReceiver());
    	}
    	if(message.getReplyID() != null)
    	{
    		hds.put(MessageHeader.HEADER_FIELD_REPLY_ID, message.getReplyID());
    	}
    	if(message.getReplyUser() != null)
    	{
    		hds.put(MessageHeader.HEADER_FIELD_REPLY_USER, message.getReplyUser());
    	}	
    	if(message.getReplyUser() != null)
    	{
    		hds.put(MessageHeader.HEADER_FIELD_MSG_TOPIC, message.getReplyUser());
    	}
    	am.setHeaders(hds);
    	return am;
    }
}
